import pygame
import os
import sys
from pygame.locals import *
pygame.init()

fpsClock = pygame.time.Clock()
# 定义游戏屏幕大小
win = pygame.display.set_mode((800, 480))
WIDTH = 400
HEIGNT = 480
WIDTH1 = 20
HEIGNT1 = 5


class BG:
    win = None
    width = WIDTH
    height = HEIGNT
    image = pygame.image.load('bg.jpg')

    def __init__(self, win):
        self.win = win

    def draw(self):
        self.win.blit(self.image, (0, 0))


class Hero:
    win = None
    width = WIDTH1
    height = HEIGNT1
    zt = 0
    ri=0
    li=0
    x=0
    y=0
    standing = pygame.image.load('standing.png')
    rImage = [pygame.image.load('Left1.png'),
              pygame.image.load('Left2.png'),
              pygame.image.load('Left3.png'),
              pygame.image.load('Left4.png'),
              pygame.image.load('Left5.png'),
              pygame.image.load('Left6.png'),
              pygame.image.load('Left7.png'),
              pygame.image.load('Left8.png'),
              pygame.image.load('Left9.png')]

    lImage = [pygame.image.load('Right1.png'),
              pygame.image.load('Right2.png'),
              pygame.image.load('Right3.png'),
              pygame.image.load('Right4.png'),
              pygame.image.load('Right5.png'),
              pygame.image.load('Right6.png'),
              pygame.image.load('Right7.png'),
              pygame.image.load('Right8.png'),
              pygame.image.load('Right9.png')]



    def __init__(self, win):
        self.win = win

    def draw(self):
        if self.zt==1:
            self.x-=5
        elif self.zt==2:
            self.x+=5
        elif self.zt==3:
            self.y-=5
        elif self.zt==4:
            self.y+=5
        if self.zt==1:
            self.win.blit(self.rImage[self.ri], (self.x, self.y))
            self.ri += 1
            if self.ri == 9:
                self.ri -= 9
        elif self.zt==2:
            self.win.blit(self.lImage[self.li], (self.x, self.y))
            self.li += 1
            if self.li == 9:
                self.li -= 9
        else:
            self.win.blit(self.standing, (self.x, self.y))
    def update(self):
        keys=pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.zt=1
        elif keys[pygame.K_RIGHT]:
            self.zt=2
        elif keys[pygame.K_UP]:
            self.zt=3
        elif keys[pygame.K_DOWN]:
            self.zt=4
        else:
            self.zt=0
bg=BG(win)
hero=Hero(win)
while True:
    bg.draw()
    hero.update()
    hero.draw()
    # 如果退出游戏
    for event in pygame.event.get():
        # 如果退出游戏
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    # 完成屏幕绘制，执行更新
    pygame.display.update()
    # 指定刷新速率：每秒30帧
    # 如遇到复杂绘制运算，要降低速率
    fpsClock.tick(30)